/*

Replication code for "Pain at the Pump, Pain at the Polls: Global Evidence on Election Timing, State Capacity, and Gasoloine Prices"
Journal of Political Institutions and Political Economy

Author:
Matthew D. Fails
fails@oakland.edu

Analysis completed in Stata 16, with two additional packages from SSC: 'interflex' and 'reghdfe'

*/


************
*Preface 
************

clear 
version 16


***********
*Read in Replication Dataset
***********

*Substitute directory details in the line of code below, and delete the asterisk to remove the comment
*use ".....\ReplicationDataPainAtPump.dta", clear

************
*Data Description

/*
The datafile contains all variables necessary to replicate the results presented in the main manuscript and the appendix. The following is a brief description of variables and their source:

VARIABLE			DESCRIPTION/SOURCE
country_name 		Country Name
countryisocode		Country ISO Code
COWcode				Correlates of War Country Code
NELDAccode			Hyde and Marinov (2012) country code
uniqueid_wb			Unique country-year-month identifier based on WB isocode
nelda_election		Dummy variable for whether an election occurred this month (Hyde and Marinov 2012)
leg_elec			Dummy variable for whether an election was a legislative election (1) or executive (0) (Hyde and Marinov 2012)
electionid			Unique election ID (Hyde and Marinov 2012)
v2x_polyarchy		Electoral Democracy / Polyarchy (VDem)
v2x_regime			Regimes of the World (Vdem)
v2clrspct			Impartial Public Administration (Vdem)
v2stfisccap			Fiscal State Capacity (Vdem)
v2xnp_regcorr		Corruption (Vdem)
log_oil				(log) of Oil Income per Capita (Ross and Mahdavi 2015)
log_reserves		(log) Proven Oil Reserves (International Energy Agency )
net_oil_gas_exports_valuepop	Net Oil and Gas Exports Value, per capita (Ross and Mahdavi 2015)
bmgap2015adj		Price Gap measure of implicit fuel tax/subsidy (Ross, Hazlett, and Mahdavi 2015)
price_usd_2015		Gasoline price per liter, in USD (Martinez-Alvarez et al. 2023)
price_comb			Extended series of Gasoline price per liter, with additional data for 2016-2018 (Author coded)
countrycode_unified	Unified country code for merging datasets	
year				year		
month				month
elec_month			count of months elapsed since last NELDA election (Hyde and Marinov 2012)
elec_month_prior	count of months preceeding next election	
pres				Dummy variable for Presidential systems (Cruz, Keefer, and Scartascini 2020)			
execrlc				Executive ideology (Cruz, Keefer, and Scartascini 2020)
pr					Dummy for Proportional Representation systems (Cruz, Keefer, and Scartascini 2020)
countrycode_wb		World Bank country code
nelda12				NELDA variable measuring whether incumbents were confident of victory before election day (Hyde and Marinov 2012)
Capacity			State Capacity (Hanson and Sigman 2021)
yr_mt				year-month identifier variable

*/


************

*Set up macros to run various permutations more efficiently 
global dv "price_usd_2015"
global dv_2 "price_comb"
global method "reghdfe"
global opt "absorb(countrycode_unified yr_mt) vce(cluster countrycode_unified)"
global opt2 "absorb(countrycode_unified year) vce(cluster countrycode_unified)"

************

***Measuring key variable of election proximity

label var elec_month "count of months elapsed since last NELDA election"
label var elec_month_prior "count of months preceeding next NELDA election"

bysort electionid: egen cycle=max(elec_month)

*generate an ID for inclusion 
gen neldaid = 1 if leg_elec!=.
tab neldaid, mis

bysort electionid: gen proximity = elec_month / cycle if neldaid==1
sort countrycode_unified year month
label var proximity "Election Proximity"

hist proximity, percent scheme(plotplain) bcolor(ebblue) xtitle("Election Proximity") title("Observed Values of Election Proximity") 

************

* Generate Figures 1 and 2

*Figure 1 - scatterplot of prices and capacity in main sample; becauase Capacity is measured in annual terms, convert prices to annual averages as well, limiting to countries with valid price data for all 12 months in a given year

*run basemodel first to store estimation sample
$method price_comb proximity Capacity c.proximity#c.Capacity if v2x_regime>=2 & nelda12=="no", $opt
*re-calculate country-year averages only if we have prices for all 12 months of the data
bysort countrycode_unified year: egen mt_ct = count(price_comb)
label var mt_ct "number of months in the year with valid price data"

bysort countrycode_unified year: egen mean_price = mean(price_comb) if e(sample) & mt_ct==12
scatter mean_price Capacity if e(sample) 

*generate alternatives that average variables over entire time period
bysort countrycode_unified: egen mean_price_panel = mean(price_comb) if e(sample)
bysort countrycode_unified: egen mean_cap = mean(Capacity) if e(sample)	

***Figure 1 in manuscript - plot two lfit lines, one for the yearly, and one averaged over the entire time sample
twoway (scatter mean_price Capacity if e(sample), mcolor(ebblue)) (lfit mean_price Capacity if e(sample), lcolor(cranberry) lstyle(solid)) (lfit mean_price_panel mean_cap if e(sample), ///
	lcolor(cranberry) legend(off) ///
	ytitle("Price Per Liter")	///
	xtitle("State Capacity" "(Hanson & Sigman 2021)") ///
	title("Figure 1: State Capacity and" "(Annual Average) Fuel Prices") ///
	aspectratio(1) ///
	)	
	
**generate these with the Vdem alternatives, for Appendix Figures A1 and A2 
twoway (scatter mean_price v2stfisccap if e(sample), mcolor(ebblue)) (lfit mean_price v2stfisccap if e(sample), ///
	lcolor(cranberry) legend(off) ///
	ytitle("Price Per Liter")	///
	xtitle("Fiscal State" "Capacity (VDem)") ///
	title("State Capacity and" "(Annual Average) Fuel Prices") ///
	aspectratio(1) ///
	)
	
twoway (scatter mean_price v2clrspct if e(sample), mcolor(ebblue)) (lfit mean_price v2clrspct if e(sample), ///
	lcolor(cranberry) legend(off) ///
	ytitle("Price Per Liter")	///
	xtitle("Administrative State" "Capacity (VDem)") ///
	title("State Capacity and" "(Annual Average) Fuel Prices") ///
	aspectratio(1) ///
	)

	
** Figure 2 
**Start by making a new proximity variable that bins into .1 increments
gen newprox = 0 if proximity==0 
replace newprox = .1 if proximity<.1 & proximity!=0
replace newprox = .2 if proximity>=.1 & proximity<.2
replace newprox = .3 if proximity>=.2 & proximity<.3
replace newprox = .4 if proximity>=.3 & proximity<.4
replace newprox = .5 if proximity>=.4 & proximity<.5
replace newprox = .6 if proximity>=.5 & proximity<.6
replace newprox = .7 if proximity>=.6 & proximity<.7 
replace newprox = .8 if proximity>=.7 & proximity<.8
replace newprox = .9 if proximity>=.8 & proximity<.9
replace newprox = 1 if proximity>=.9 & proximity<1

*rescale by 10 so I can attach value labels
gen newprox2 = newprox*10

bysort newprox2: egen mean_price_low2 = mean(price_comb) if Capacity<-.039 & e(sample)
twoway line mean_price_low2 newprox
bysort newprox2: egen mean_price_high = mean(price_comb) if Capacity>2.05 & e(sample)
twoway line mean_price_high newprox

label define new_axis 0 "Election Month" 5 "Election cycle midpoint" 10 "Month Before Election"
label values newprox2 new_axis

*limited to if e(sample), from prior call to main estimation model
twoway (line mean_price_high newprox2 if newprox2<=10 & e(sample), lcolor(cranberry)) (line mean_price_low2 newprox2 if newprox2<=10 & e(sample), lstyle(solid) lcolor(ebblue) ylabel(.75(.25)2) ytitle("Average Price Per Liter") xtitle(" ") xlabel(0 5 10, valuelabel angle(-45)) ///
	aspectratio(1) text(1.7 6 "Top 10% Capacity Countries", size(vsmall)) text(1.1 5 "Bottom 10% Capacity Countries", size(vsmall)) legend(off) ///
	title("Figure 2: Election Timing," "State Capacity, and Fuel Prices" " ", size(med)) ///
	)

************
	
*Main Regression Results 

$method price_comb proximity Capacity c.proximity#c.Capacity if v2x_regime>=2 & nelda12=="no", $opt
est store m1
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil if v2x_regime>=2 & nelda12=="no", $opt
est store m2
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", $opt
est store m3

**make table
esttab m1 m2 m3 using t1.rtf, nogaps comp se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label nocons ///
note("NOTE: Dependent variable is measure of gasoline price per liter. Standard errors clustered on countries in parentheses." "* p < 0.10, ** p < 0.05, *** p < 0.01. Sample is democratic regimes, observed monthly from 1990-2015. Country and" "year-month fixed effects and constant also included.") replace
	
************	

*Main Regression Specification, Robustness
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", $opt
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy pres if v2x_regime>=2 & nelda12=="no", $opt
est store m4
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy pr if v2x_regime>=2 & nelda12=="no", $opt
est store m5
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy execrlc if v2x_regime>=2 & nelda12=="no", $opt
est store m6
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy v2xnp_regcorr if v2x_regime>=2 & nelda12=="no", $opt
est store m7
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy net_oil_gas_exports_valuepop if v2x_regime>=2 & nelda12=="no", $opt
est store m8
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no" & leg_elec!=1, $opt
est store m10
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no" & leg_elec==1, $opt
est store m11
$method bmgap2015adj proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no" , $opt
est store m12
$method bmgap2015adj proximity Capacity c.proximity#c.Capacity loggdppc log_reserves v2x_polyarchy if v2x_regime>=2 & nelda12=="no" , $opt
est store m13
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", absorb(countrycode_unified yr_mt) vce(cluster countrycode_unified yr_mt)
est store m14

*Generate raw materials for Table 2 in paper
esttab m4 m5 m6 m7 m10 m11 using t2.rtf, nogaps comp se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label nocons ///
note("NOTE: Dependent variable is measure of gasoline price per liter. Standard errors clustered on countries in parentheses." "* p < 0.10, ** p < 0.05, *** p < 0.01. Sample is democratic regimes, observed monthly from 1990-2015. Country and" "year-month fixed effects and constant also included.") replace
	
*Generate raw materials for Table A2 in Appendix	
esttab m8 m12 m13 m14 using t3.rtf, nogaps comp se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label nocons ///
note("NOTE: Dependent variable is measure of gasoline price per liter. Standard errors clustered on countries in parentheses." "* p < 0.10, ** p < 0.05, *** p < 0.01. Sample is democratic regimes, observed monthly from 1990-2015. Country and" "year-month fixed effects and constant also included.") replace
	
************

*Figure 3 - Marginal Effect plot of main regression results (focus on model 3 from table)
interflex price_comb proximity Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", fe(countrycode_unified yr_mt) vce(cluster) cluster(countrycode_unified) type(linear) ylabel("Price Per Liter") xlabel("State Capacity (Hanson & Sigman 2021)") dlabel("Election Proximity") title("Marginal Effect of Election Proximity" "on Price Per Liter")


*Regression Results and Marginal Effect plots of robustness to two VDem alternative capacity measures, presented in Appendix as Table A1 and Figures A3 and A4
$method price_comb proximity v2clrspct c.proximity#c.v2clrspct loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", $opt
est store m14
$method price_comb proximity v2stfisccap c.proximity#c.v2stfisccap loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", $opt
est store m15
	
esttab m14 m15 using t4.rtf, nogaps comp se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label nocons ///
note("NOTE: Dependent variable is measure of gasoline price per liter. Standard errors clustered on countries in parentheses." "* p < 0.10, ** p < 0.05, *** p < 0.01. Sample is democratic regimes, observed monthly from 1990-2015. Country and" "year-month fixed effects and constant also included.") replace
		
interflex price_comb proximity v2clrspct loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", fe(countrycode_unified yr_mt) vce(cluster) cluster(countrycode_unified) type(linear) ylabel("Price Per Liter") xlabel("Administrative State Capacity (VDem)") dlabel("Election Proximity") title("Marginal Effect of Election Proximity" "on Price Per Liter") 

interflex price_comb proximity v2stfisccap loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", fe(countrycode_unified yr_mt) vce(cluster) cluster(countrycode_unified) type(linear) ylabel("Price Per Liter") xlabel("Fiscal State Capacity (VDem)") dlabel("Election Proximity") title("Marginal Effect of Election Proximity" "on Price Per Liter") 


******Examination of non-linear interaction effects, for three measures of state capacity, presented in Appendix as Figures A5, A6, and A7
interflex price_comb proximity Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", fe(countrycode_unified yr_mt) vce(cluster) cluster(countrycode_unified) type(binning) ylabel("Price Per Liter") xlabel("State Capacity (Hanson & Sigman 2021)") dlabel("Election Proximity") title("Marginal Effect of Election Proximity" "on Price Per Liter")

interflex price_comb proximity v2clrspct loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", fe(countrycode_unified yr_mt) vce(cluster) cluster(countrycode_unified) type(binning) ylabel("Price Per Liter") xlabel("Administrative State Capacity (VDem)") dlabel("Election Proximity") title("Marginal Effect of Election Proximity" "on Price Per Liter") 

interflex price_comb proximity v2stfisccap loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", fe(countrycode_unified yr_mt) vce(cluster) cluster(countrycode_unified) type(binning) ylabel("Price Per Liter") xlabel("Fiscal State Capacity (VDem)") dlabel("Election Proximity") title("Marginal Effect of Election Proximity" "on Price Per Liter") 


************

*Examination of sample using elections where incumbents were confident of victory before polling (nelda12 == 'yes')

*Start by repeating main models from Table 1 above
$method price_comb proximity Capacity c.proximity#c.Capacity if v2x_regime>=2 & nelda12=="no", $opt
est store m1
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil if v2x_regime>=2 & nelda12=="no", $opt
est store m3
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="no", $opt
est store m5

*repeat these three models, but defining sample only as elections where incumbents were confident of victory ahead of time
$method price_comb proximity Capacity c.proximity#c.Capacity if v2x_regime>=2 & nelda12=="yes", $opt
est store m2
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil if v2x_regime>=2 & nelda12=="yes", $opt
est store m4
$method price_comb proximity Capacity c.proximity#c.Capacity loggdppc log_oil v2x_polyarchy if v2x_regime>=2 & nelda12=="yes", $opt
est store m6


**make Table A5 in Appendix, contrasting uncertain and certain samples, working through additional covariates
esttab m1 m2 m3 m4 m5 m6 using t1.rtf, nogaps comp se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label nocons ///
note("NOTE: Dependent variable is measure of gasoline price per liter. Standard errors clustered on countries in parentheses." "* p < 0.10, ** p < 0.05, *** p < 0.01. Sample is democratic regimes, observed monthly from 1990-2015. Country and" "year-month fixed effects and constant also included.") replace
		
************

*Plot distribution of main variable election proximity
*first run main regression to capture sample
$method price_comb proximity Capacity c.proximity#c.Capacity if v2x_regime>=2 & nelda12=="no", $opt
hist proximity if e(sample), percent scheme(plotplain) bcolor(ebblue) xtitle("Election Proximity") title("Observed Values of Election Proximity" "In Main Estimation Sample") ytitle("Percent of Country-Month" "Observations") note("NOTE: Election Proximity measured by dividing the current month's position" "in an election cycle by the number of total months between elections. A value" "of 0 indicates an election occurred this month, a value of 0.5 indicates the" "mid-point of an election cycle, and a value of 1.0 indicates the last month" "of an election cycle.")

************

*Additional Materials Presented in Section 5, Online Appendix

*Plot Share of elections that are competitive in democracies and autocracies
bysort year: gen sum_elec = sum(nelda_election) if v2x_regime>=2
tab sum_elec
bysort year: egen max_elec = max(sum_elec)
tab max_elec
list electionid if nelda_election==1 & year ==2019 & v2x_regime>=2
list electionid if nelda_election==1 & year ==2010 & v2x_regime>=2

bysort year: egen sum_elec_un = sum(nelda_election) if v2x_regime>=2 & nelda12=="no"
bysort year: egen max_elex_un = max(sum_elec_un)

gen share_uncertain = max_elex_un / max_elec
gen share_confident = 1 - share_uncertain

graph bar share_confident share_uncertain if year<=2020, over(year, label(ang(-45))) bar(1, color(gs5)) bar(2, color(gs12)) stack ///
legend(pos(6) order(1 "Incumbents Confident of Victory" 2 "Incumbents Not Confident of Victory")) scheme(white_ptol) title("Elections in Democracies, 1990-2020") note("NOTE: Bars report annual share of democratic elections, according to whether or not" "incumbents were confident of victory ahead of time. Data from Hyde and Marinov (2012).") 

***Repeat this process, but focus on Electoral Authoritarian regimes (v2x_regime == 1)
drop sum_elec max_elec sum_elec_un max_elex_un share_uncertain share_confident

bysort year: gen sum_elec = sum(nelda_election) if v2x_regime==1
tab sum_elec
bysort year: egen max_elec = max(sum_elec)
tab max_elec
list year if max_elec==52
list electionid if nelda_election==1 & year ==2019 & v2x_regime==1
list electionid if nelda_election==1 & year ==2010 & v2x_regime==1

bysort year: egen sum_elec_un = sum(nelda_election) if v2x_regime==1 & nelda12=="no"
bysort year: egen max_elex_un = max(sum_elec_un)

gen share_uncertain = max_elex_un / max_elec
gen share_confident = 1 - share_uncertain

graph bar share_confident share_uncertain if year<=2020, over(year, label(ang(-45))) bar(1, color(gs5)) bar(2, color(gs12)) stack ///
legend(pos(6) order(1 "Incumbents Confident of Victory" 2 "Incumbents Not Confident of Victory")) scheme(white_ptol) title("Elections in Electoral Authoritarian Regimes, 1990-2020") note("NOTE: Bars report annual share of elections according to whether or not" "incumbents were confident of victory ahead of time. Data from Hyde and Marinov (2012).") 

************

*Generate summary / descriptive statistics for Appendix Table A5
$method price_comb proximity Capacity if v2x_regime>=2 & nelda12=="no", $opt
sum price_comb nelda_election proximity Capacity v2clrspct v2stfisccap loggdppc log_oil v2x_polyarchy pres pr execrlc v2xnp_regcorr net_oil_gas_exports_valuepop  leg_elec log_reserves bmgap2015adj if e(sample)

****Small empirical points made in the text
sum price_comb if v2x_regime>=2 
sum price_comb if v2x_regime<2

sum price_comb if v2x_regime>=2 & year>2003
sum price_comb if v2x_regime<2 & year>2003

centile price_comb if e(sample), centile(10 90)
*3.5 fold increase from the bottom 10% to top 10% in the estimation sample




	